package dnl.dev.memlog.sampler;

import java.io.File;

import dnl.dev.memlog.MemlogProject;
import dnl.infra.cli.CLI;
import dnl.infra.cli.CliSetting;
import dnl.infra.cli.OptionFactory;

/**
 * 
 * @author Daniel Orr
 * 
 */
public class SamplerCli {

	public static void main(String[] args) {

		CLI cli = new CLI("memlog-sampler");
		CliSetting pidSetting = OptionFactory.createMandatorySetting("pid", "process id");
		cli.addSetting(pidSetting);
		CliSetting samplerIdSetting = OptionFactory
				.createMandatorySetting("sampleid", "sampler id");
		cli.addSetting(samplerIdSetting);
		CliSetting rateSetting = OptionFactory.createMandatorySetting("rate", "in minutes (Default is 10)");
		cli.addSetting(rateSetting);

		cli.parseCli(args);

		MemlogProject memlogProject = null;

		String sampleid = cli.getString("sampleid");
		File sampleDir = new File(sampleid);
		if (sampleDir.exists()) {
			System.err.println("directory for given sample-id already exists.");
			System.exit(0);
		}
		memlogProject = new MemlogProject(sampleid);
		SamplerConfiguration configuration = new SamplerConfiguration();
		
		int rate = 10*60;// 10 minutes
		if(cli.isOptionSet("rate")){
			rate = Integer.parseInt(cli.getString("rate"));
		}
		configuration.setSamplingFrequencySec(rate);
		MemSampler memSampler = new MemSampler(cli.getString("pid"), configuration, memlogProject);
		memSampler.startSamplingThread();
	}

	protected static String resolveAutoProjectName() {
		File projFile = null;
		for (int i = 1; projFile == null; i++) {
			File f = new File("memlog-project" + i);
			if (!f.exists()) {
				return f.getName();
			}
		}
		return null;
	}
}
